﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using 开单.ComMethod;

namespace ClientApplication.ComControl
{
    public partial class UClistMore : UserControl
    {
        MyListItem List =null;
        public delegate void ListEventHandler(MyListItem myList);
        public event ListEventHandler ListEvent;
        public UClistMore()
        {
            InitializeComponent();
            listBox.KeyDown += new KeyEventHandler(listBox_KeyDown);
        }

        void listBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                ListBox lb = (ListBox)sender;
                if (lb.Text == "") { return; }
                List = new MyListItem(lb.SelectedValue.ToString(),lb.Text);
                if (ListEvent != null) { ListEvent(List);}
                this.Visible = false;
            }
            if (e.KeyCode == Keys.Escape) { this.Visible = false; }
        }

        public void addData(DataTable dt)
        {
            if (dt == null) { MessageBox.Show("不存在"); return; }
            if (dt.Rows.Count==0) { MessageBox.Show("未查到相关数据,请重试"); return; }
            listBox.DataSource=dt;
            listBox.DisplayMember=dt.Columns[1].ToString();
            listBox.ValueMember = dt.Columns[0].ToString();

            this.Visible = true;
            this.BringToFront();
            listBox.Focus();
        }

        private void listBox_DoubleClick(object sender, EventArgs e)
        {
            ListBox lb = (ListBox)sender;
            if (lb.Text == "") { return; }
            List = new MyListItem(lb.SelectedValue.ToString(), lb.Text);
            if (ListEvent != null) { ListEvent(List); }
            this.Visible = false;
        }
        
    }
}
